package com.nuoniu.sibanyun.controller.production;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.production.ErpBomMain;
import com.nuoniu.sibanyun.service.production.IVBomMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  bom前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-01-08
 */
@RestController
@RequestMapping("/bom")
@Api(value = "bom管理", tags = "bom管理")
public class VBomMainController extends BaseController {

    @Autowired
    private IVBomMainService bomMainService;

    @GetMapping("getList")
    @ApiOperation("获取列表")
    public HttpResult getList(Page page, ErpBomMain bomMain) {
        return HttpResult.ok(bomMainService.getList(page, bomMain));
    }

    @ApiOperation("获取bom明细列表")
    @GetMapping("getChildBomList")
    public HttpResult getChildBomList(Long bomId) {
        return HttpResult.ok(bomMainService.getChildBomList(bomId));
    }

    @ApiOperation("保存bom")
    @PostMapping("save")
    public HttpResult save(@RequestBody ErpBomMain bomMain) {
        if(bomMain == null || StringUtils.isBlank(bomMain.getVersionCode())) {
            throw new NuoNiuException("版本号不能为空");
        }

        if(bomMain.getInventoryId() == null) {
            throw new NuoNiuException("母件不能为空");
        }

        return HttpResult.ok(bomMainService.saveBom(bomMain));
    }

    @ApiOperation("删除Bom")
    @DeleteMapping("delete")
    public HttpResult deleteBom(Long id) {
        bomMainService.deleteById(id);
        return HttpResult.ok();
    }

    /**
     * 审核/弃审
     * @author 小啊刚
     * @date 2020-11-25
     */
    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "单据id"),
        @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        bomMainService.examine(id, status);
        return HttpResult.ok();
    }

}
